今天在部署hexo博客时,hexo d后出现”Error: Spawn failed“报错日志。
这表明在执行hexo d时,Hexo无法通过SSH连接到GitHub,导致博客文件部署失败,新文章未能推送到远程仓库。
具体解决办法如下。

报错日志

报错日志一般为以下内容:

1
2
3
4
5
6
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (D:\Blog\node_modules\hexo-deployer-git\node_modules\hexo-util\lib\spawn.js:51:21)
at ChildProcess.emit (node:events:519:28)
at cp.emit (D:\Blog\node_modules\cross-spawn\lib\enoent.js:34:29)
at ChildProcess._handle.onexit (node:internal/child_process:294:12)

解决办法

测试 SSH 连接

  1. 打开Git Bash(或 CMD/PowerShell),运行:
1
ssh -T git@github.com
  1. 可能结果
  • 成功:输出 Hi ! You’ve successfully authenticated, but GitHub does not provide shell access.,表示SSH配置正确,问题可能出在网络或Hexo配置。
  • 失败:输出 ssh: connect to host github.com port 22: Connection refused 或类似错误,继续以下步骤。

检查网络和防火墙

  1. 测试网络连接,运行:
1
ping github.com
  • 如果返回类似Reply from 140.82.121.4,表示网络可达GitHub。
  • 如果失败,检查 DNS 或网络设置。
  1. 检查22端口,运行:
1
telnet github.com 22
  • 成功:显示 Connected to github.com 或类似信息。
  • 失败:显示 Connection refused 或 Connection timed out 表明 22 端口被防火墙或 ISP 屏蔽。
    我在此处提示报错:
1
2
3
4
5
6
7
telnet : 无法将“telnet”项识别为
cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ telnet github.com 22
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (telnet:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

主要问题:ssh: connect to host github.com port 22: Connection refused。
ping github.com结果显示网络可以访问GitHub,但SSH连接失败,表明22端口可能被防火墙或网络限制屏蔽。

使用 SSH 过 443 端口

  1. 编辑 SSH 配置文件
    打开(或新建)C:\Users<你的用户名>.ssh\config,并添加:
1
2
3
4
5
6
Host github.com
HostName ssh.github.com
User git
Port 443
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

保存并关闭。

  1. 测试连接,运行:
1
ssh -T git@github.com

我这里提示的是:

1
2
3
The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
......一串类似密匙和文件路径的内容(避免泄露隐私就不显示出来了)
Are you sure you want to continue connecting (yes/no/[fingerprint])?

直接输入yes并按回车键即可解决问题。

重新部署

接下来应该就没问题了,直接运行:

1
hexo cl && hexo g && hexo d

我到这里就已经解决所有问题了,但不保证该方法能够完全有效,可以先尝试看看。